package com.pgytesting.secondhandcar.service.impl;

import com.pgytesting.secondhandcar.model.Power;
import com.pgytesting.secondhandcar.model.Role;
import com.pgytesting.secondhandcar.model.RolePowerRelation;
import com.pgytesting.secondhandcar.dao.RolePowerRelationDao;
import com.pgytesting.secondhandcar.service.PowerService;
import com.pgytesting.secondhandcar.service.RolePowerRelationService;
import com.pgytesting.secondhandcar.util.SessionUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (RolePowerRelation)表服务实现类
 *
 * @author makejava
 * @since 2019-08-16 15:20:21
 */
@Service("rolePowerRelationService")
public class RolePowerRelationServiceImpl implements RolePowerRelationService {
    @Resource
    private RolePowerRelationDao rolePowerRelationDao;
    @Autowired
	private PowerService powerService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public RolePowerRelation queryById(Integer id) {
        return this.rolePowerRelationDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<RolePowerRelation> queryAllByLimit(int offset, int limit) {
        return this.rolePowerRelationDao.queryAllByLimit(offset, limit);
    }
    
    /**
     * 条件查询多条数据
     *
     * @param rolePowerRelation 实例对象
     * @return 对象列表
     */
    @Override
    public List<RolePowerRelation> queryAll(RolePowerRelation rolePowerRelation) {
    	rolePowerRelation.setStatus(0);
        return this.rolePowerRelationDao.queryAll(rolePowerRelation);
    }

    /**
     * 新增数据
     *
     * @param rolePowerRelation 实例对象
     * @return 实例对象
     */
    @Override
    public RolePowerRelation insert(RolePowerRelation rolePowerRelation) {
        this.rolePowerRelationDao.insert(rolePowerRelation);
        return rolePowerRelation;
    }

    /**
     * 修改数据
     *
     * @param rolePowerRelation 实例对象
     * @return 实例对象
     */
    @Override
    public RolePowerRelation update(RolePowerRelation rolePowerRelation) {
        this.rolePowerRelationDao.update(rolePowerRelation);
        return this.queryById(rolePowerRelation.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.rolePowerRelationDao.deleteById(id) > 0;
    }

	@Override
	public void roleRelationPower(Role role) {
		RolePowerRelation rpr = new RolePowerRelation();
		rpr.setRoleId(role.getId());
		rpr.setStatus(-1);
		rpr.setUpdatedBy(SessionUtil.getSessionAdmin().getUserNumber());
		rolePowerRelationDao.update(rpr);
		
		RolePowerRelation rp = new RolePowerRelation();
		rp.setRoleId(role.getId());
		rp.setStatus(0);
		rp.setCreatedBy(SessionUtil.getSessionAdmin().getUserNumber());
		for(int i=0;i<role.getPowerIdList().size();i++) {
			rp.setPowerId(role.getPowerIdList().get(i));
			rolePowerRelationDao.insert(rp);
		}
	}

	@Override
	public Boolean checkUserPower(Integer roleId, String url) {
		Power power = new Power();
		power.setInterfaceAddress(url);
		power.setStatus(0);
		Power p = powerService.queryObj(power);
		if(p == null) {
			return false;
		}
		
		RolePowerRelation rp = new RolePowerRelation();
		rp.setPowerId(p.getId());
		rp.setRoleId(roleId);
		rp.setStatus(0);
		RolePowerRelation r = rolePowerRelationDao.queryObj(rp);
		if(r == null) {
			return false;
		}
		
		return true;
	}
}